<template>
  <div class="app-container">
    <!-- 查询 -->
    <el-form :model="queryParams" ref="queryRefs" :inline="true">
      <el-form-item label="流程名称" prop="noticeTitle">
        <el-input
          v-model="queryParams.flowName"
          placeholder="请输入流程名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 操作列 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="tableData"
      style="width: 100%; margin: 20px 0"
    >
      <!-- <el-table-column label="序号" :index="indexMethod" align="center" /> -->
      <el-table-column prop="id" v-if="false" />
      <el-table-column prop="name" label="流程名称" align="center" />
      <el-table-column prop="startUserName" label="发起人" align="center" />
      <el-table-column prop="taskName" label="节点名称" align="center" />
      <el-table-column prop="flowStatus" label="流程状态" align="center">
        <template #default="scope">
          <el-tag v-if="scope.row.flowStatus == 1">审批中</el-tag>
          <el-tag type="success" v-if="scope.row.flowStatus == 4"
            >审批通过</el-tag
          >
          <el-tag type="danger" v-if="scope.row.flowStatus == 3"
            >审批驳回</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="startTime" label="任务到达时间" align="center" />
      <el-table-column fixed="right" label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            size="small"
            @click="openDrawer(scope.row)"
          >
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="float: right"
      layout="prev, pager, next"
      v-model:page-size="queryParams.pageSize"
      v-model:current-page="queryParams.pageNum"
      :total="pageInfo.totals"
      @current-change="initData()"
    />
    <el-drawer
      title="审批详情"
      v-model="previewVisible"
      direction="rtl"
      size="35%"
      destroy-on-close
    >
      <ApprovalDetails
        :form-data="formData"
        :his-list="flowHisList"
        :proceed-tasks="proceedTasks"
        :flowStatus="flowStatus"
        :processInstanceId="instanceId"
        :startUser="startUser"
        :startTime="startTime"
      ></ApprovalDetails>
    </el-drawer>
  </div>
</template>
<script setup>
import { ref, reactive, toRefs, watch } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { getFlowDoneList, getFlowInstanceInfo } from "../api/flow.js";

const loading = ref(true);
const router = useRouter();
const tableData = ref([]);
const previewVisible = ref(false);
const formData = ref("");
const formItem = ref("");
const flowId = ref("");
const flowHisList = ref([]);
const proceedTasks = ref([]);
const instanceId = ref("");
const flowStatus = ref("");
const startUser = ref({});
const startTime = ref("");
const taskId = ref("");
import ApprovalDetails from "./flow/approval-details.vue";
const formBuilderRef = ref(null);
const store = useStore();
const queryData = reactive({
  from: {},
  queryParams: {
    pageNum: 1,
    pageSize: 5,
    flowName: undefined,
  },
  rules: {
    flowName: [
      { required: true, message: "公告标题不能为空", trigger: "blur" },
    ],
  },
});

/*数据封装区域*/
let pageInfo = reactive({
  pageNum: 1,
  pageSize: 5,
  totals: Number,
});

const { from, queryParams, rules } = toRefs(queryData);
const indexMethod = (index) => {
  return index + 1;
};

const toFlowDetail = (row) => {
  router.push("/flowEdit?id=" + row.id);
};

const openDrawer = (row) => {
  getFlowInstanceInfo({ processInstanceId:row.procInstId,taskId:""})
    .then((res) => {
      if (res.status === 0) {
        formItem.value = res.data.formItem;
        formData.value = res.data.formData;
        flowHisList.value = res.data.list;
        proceedTasks.value = res.data.proceedTasks;
        instanceId.value = res.data.processInstanceId;
        flowStatus.value = res.data.flowStatus;
        startUser.value = res.data.startUser;
        startTime.value = res.data.startTime;
        store.dispatch("form/updateConfig", JSON.parse(res.data.formItem));
        previewVisible.value = true;
      }
    })
    .catch(function (error) {
      console.log(error);
    });
};

const handleAdd = () => {
  router.push("/flowAdd");
};

const initData = () => {
  loading.value = true;
  getFlowDoneList(queryParams.value).then((res) => {
    console.log("查看已办:", res);
    if (res.status === 0) {
      console.log(res.data);
      tableData.value = res.data.rows;
      pageInfo.totals = res.data.total;
    }
    loading.value = false;
  });
};

const handlerSubForm = () => {
  console.log("handlerSubForm", formData.value);
};

initData();

const handleQuery = () => {
  console.log("handleQuery");
};

const handleClick = () => {
  console.log("click");
};
watch(
  formData,
  (newValue, oldValue) => {
    console.log("modelValue-----------改变了", newValue);
  },
  { immediate: formData }
);
</script>
    
    <style>
.app-container {
  padding: 20px;
}
</style>